package hn.cch.springboot_redisson.distributed;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class DistributedLock {

    @Autowired
    private RedissonClient redissonClient;

    public RLock lock(String lockKey){
        RLock rLock = redissonClient.getLock(lockKey);
        rLock.lock();
        return rLock;
    }

    public RLock lock(String lockKey, int leaseTime, TimeUnit unit){
        RLock rLock = redissonClient.getLock(lockKey);
        rLock.lock(leaseTime, unit);
        return rLock;
    }

    public RLock unlock(String lockKey){
        RLock rLock = redissonClient.getLock(lockKey);
        rLock.unlock();
        return rLock;
    }

}
